package org.clockwise.feature;

import java.util.Arrays;

import org.clockwise.nlpir.ResultParser;


/**
 * This is the class storing all information about a user.
 * @author WangYan
 *
 */
public class Account {
	public String name;				// the name of the account
	public Long id;					// the id of the account
	public String info;				// the officially verified information of the account
	public String[] tags;			// the list of tags defined by user
	
	public String[] nameSegWord;
	public String[] nameSegLabel;
	public String[] infoSegWord;	
	public String[] infoSegLabel;
//	public String[] tagsSegWord;
//	public String[] tagsSegLabel;
	
	public int inlink;				// the number of fans
	public int outlink;				// the number of followed users
	public int publish;				// the number of micro blog publications
	
	// format of a legal line is: (separated by '\t') 
	//		id	publish	out	in	name	info (or " *** ")	:	tag1	tag2	tag3	...
	// rank 0	1		2	3	4		5			
	// warning: some info may contain '\t' as well, so ':' is a necessary separator
	
	/*
	 * This constructor is deprecated, for it is used for the older version
	 * of input file.
	 */
	/*
	public Account(String line) {
		String[] segment = line.split("\t");
//		System.out.println(segment.length);
		try {
			id = Long.parseLong(segment[0]);
			publish = Integer.parseInt(segment[1]);
			outlink = Integer.parseInt(segment[2]);
			inlink  = Integer.parseInt(segment[3]);
			name = segment[4].toLowerCase();
			info = "";
			int i = 5;
			while (!segment[i].equals(":")) {
				info += segment[i++].toLowerCase();
			}
			tags = Arrays.copyOfRange(segment, i + 1, segment.length);
			for (i = 0; i < tags.length; i++) {
				tags[i] = tags[i].toLowerCase();
			}
//			System.out.println("\t" + tags.length);
		} catch (NumberFormatException e) {
			System.out.println("Illegal line: " + line);
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Illegal line: " + line);
		}
	}
	*/
	
	/*
	 * Format of legal line is: (seperated by '\t')
	 * 	id publish out in name #nameseg# info #infoseg# : tag1 tag2 tag3 ...
	 *  0  1       2   3  4    5         6    7         8 9    
	 */
	public Account(String line) {
		String[] segment = line.split("\t");
		if (segment.length < 9) {
			System.out.println("Illegal line of account info");
		}
		else {
			try {
				this.id 	 = Long.parseLong(segment[0]);
				this.publish = Integer.parseInt(segment[1]);
				this.outlink = Integer.parseInt(segment[2]);
				this.inlink  = Integer.parseInt(segment[3]);
				this.name	 = segment[4];
				if (segment[5].startsWith("#") && segment[5].endsWith("#")) {
					String temp = segment[5].substring(1, segment[5].length() - 1);
					String[][] nameseg = ResultParser.parse(temp);
					this.nameSegWord = nameseg[0];
					this.nameSegLabel = nameseg[1];
				}
				this.info	 = segment[6];
				if (segment[7].startsWith("#") && segment[7].endsWith("#")) {
					String temp = segment[7].substring(1, segment[7].length() - 1);
					String[][] infoseg = ResultParser.parse(temp);
					this.infoSegWord = infoseg[0];
					this.infoSegLabel = infoseg[1];
				}
				if (segment[8].equals(":")) {
					if (segment.length > 9) {
						this.tags = Arrays.copyOfRange(segment, 9, segment.length);
					}
					else {
						this.tags = new String[1];
						this.tags[0] = "";
					}
				}
			} catch (NumberFormatException e) {
				System.out.println("Wrong number format");
			}
		}
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		StringBuilder temp = new StringBuilder();
		sb.append(this.id);
		sb.append("\t");
		sb.append(this.publish);
		sb.append("\t");
		sb.append(this.outlink);
		sb.append("\t");
		sb.append(this.inlink);
		sb.append("\t");
		sb.append(this.name);
		sb.append("\t#");
		for (int i = 0; i < this.nameSegWord.length; i++) {
			if (this.nameSegWord[i] == null || this.nameSegWord[i].length() == 0 || this.nameSegWord[i].contains(" ")) {
				continue;
			}
			temp.append(this.nameSegWord[i]);
			temp.append("/");
			temp.append(this.nameSegLabel[i]);
			temp.append(" ");
		}
		sb.append(temp.toString().trim());
		temp = new StringBuilder();
		sb.append("#\t");
		sb.append(this.info);
		sb.append("\t#");
		for (int i = 0; i < this.infoSegLabel.length; i++) {
			if (this.infoSegWord[i] == null || this.infoSegWord[i].length() == 0 || this.infoSegWord[i].contains(" ")) {
				continue;
			}
			temp.append(this.infoSegWord[i]);
			temp.append("/");
			temp.append(this.infoSegLabel[i]);
			temp.append(" ");
		}
		sb.append(temp.toString().trim());
		sb.append("#\t:\t");
		for (int i = 0; i < this.tags.length; i++) {
			sb.append(this.tags[i]);
			if (i + 1 != this.tags.length) {
				sb.append("\t");
			}
		}
		return sb.toString();
	}
	
	
	/*	Deprecated
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.name);
		sb.append(":");
		sb.append("微博");
		sb.append(this.publish);
		sb.append("，关注");
		sb.append(this.outlink);
		sb.append("，粉丝");
		sb.append(this.inlink);
		sb.append("，");
		sb.append(this.info);
		return sb.toString();
	}
	*/
}
